package cn.chinamobile.controller;


import cn.chinamobile.entities.CommonResult;
import cn.chinamobile.entities.User;
import cn.chinamobile.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j
@CrossOrigin
public class UserController {

    @Resource
    private UserService userService;


    @RequestMapping(value = "/user/register", method = RequestMethod.POST)
    public CommonResult register(@RequestBody User user) {
        User registerUser = userService.getUserByUsername(user.getUsername());
        if (registerUser != null) {
            return new CommonResult(400, "该用户名已经存在！");
        }
        int result = userService.userRegister(user);
        return new CommonResult(200, "注册成功！");
    }

    @RequestMapping(value = "/user/login", method = RequestMethod.POST)
    public CommonResult login(@RequestBody User user) {
        User loginUser = userService.getUserByUsername(user.getUsername());

        if (loginUser == null) {
            return new CommonResult(400, "该用户不存在");
        }
        if (loginUser.getPassword().equals(user.getPassword()) && loginUser.getIdentity() == user.getIdentity()) {
            return new CommonResult(200, "登录成功！", loginUser);
        } else {
            return new CommonResult(400, "登录失败，密码不正确！");
        }

    }




}
